﻿using System;
using System.Collections.Generic;
using System.Text;
using Castle.ActiveRecord;
using System.Collections;
using System.Data;
using Castle.ActiveRecord.Queries;
using System.ComponentModel;
using Mang24k99.Model.GOLD;
using Mang24k99.Model.PUBC;

using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord;
using Castle.Components.Validator;
using Mang24k99.Model.CLMM;


namespace Mang24k99.Model.COMM
{
    [ActiveRecord("KT_COMM_CODELIST")]
    public class MKtCommCodeList:ActiveRecordBase
    {
        #region Model

        private int _id;
        private int? _code_no;
        private string _code_cname;
        private string _code_ename;
        private IList<MKtGoldGoldbreed> _MKtGoldGoldbreed;
        private IList<MKtGoldProduct> _MKtGoldProduct;
        private IList<MKtPubcCyc> _MKtPubcCyc;
        private IList<MKtCommTreemenu> _MKtCommTreemenu;
		private IList<MKtClmmOrder> _MChildKtClmmOrderByFormNO;
        private MKtCommCodeType _MKtCommCodeType;
        private string _code_comments;
        private int? _is_validate;
        private DateTime? _input_time;
        private DateTime? _modify_time;
        private DateTime? _delete_time;


        [HasMany(typeof(MKtGoldGoldbreed), ColumnKey = "BREED_TYPE", Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse = true, Lazy = true)]
        public IList<MKtGoldGoldbreed> MKtGoldGoldbreed
        {
            get
            {
                return _MKtGoldGoldbreed;
            }
            set
            {
                _MKtGoldGoldbreed = value;
            }
        }
        [HasMany(typeof(MKtGoldProduct), ColumnKey = "STANDARD_NO", Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse = true, Lazy = true)]
        public IList<MKtGoldProduct> MKtGoldProduct
        {
            get
            {
                return _MKtGoldProduct;
            }
            set
            {
                _MKtGoldProduct = value;
            }
        }
        [HasMany(typeof(MKtPubcCyc), ColumnKey = "CYC_TYPE", Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse = true, Lazy = true)]
        public IList<MKtPubcCyc> MKtPubcCyc
        {
            get
            {
                return _MKtPubcCyc;
            }
            set
            {
                _MKtPubcCyc = value;
            }
        }
        [HasMany(typeof(MKtCommTreemenu), ColumnKey = "OPRATE_TYPE", Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse = true, Lazy = true)]
        public IList<MKtCommTreemenu> MKtCommTreemenu
        {
            get
            {
                return _MKtCommTreemenu;
            }
            set
            {
                _MKtCommTreemenu = value;
            }
        }

		[HasMany(typeof(MKtClmmOrder), ColumnKey = "FORM_NO", Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse = true, Lazy = true)]
		public IList<MKtClmmOrder> MChildKtClmmOrderByFormNO
		{
			get
			{
				return _MChildKtClmmOrderByFormNO;
			}
			set
			{
				_MChildKtClmmOrderByFormNO = value;
			}
		}
        /// <summary>
        /// ID
        /// </summary>
        [Property("ID", Update = false, Insert = false)] 
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        
        /// <summary>
        /// 代码编号
        /// </summary>
        [PrimaryKey(PrimaryKeyType.Assigned, "CODE_NO")]
        public int? CODE_NO
        {
            set { _code_no = value; }
            get { return _code_no; }
        }

        /// <summary>
        ///代码中文名称 
        /// </summary>
        [Property("CODE_CNAME")]
        public string CODE_CNAME
        {
            set { _code_cname = value; }
            get { return _code_cname; }
        }

        /// <summary>
        ///代码英文名称 
        /// </summary>
        [Property("CODE_ENAME")]
        public string CODE_ENAME
        {
            set { _code_ename = value; }
            get { return _code_ename; }
        }

        /// <summary>
        /// 类型编码
        /// </summary>
        [BelongsTo("TYPE_CODE")]
        public MKtCommCodeType MKtCommCodeType
        {
            set { _MKtCommCodeType = value; }
            get { return _MKtCommCodeType; }
        }

     
        /// <summary>
        /// 备注
        /// </summary>
        [Property("CODE_COMMENTS")]
        public string CODE_COMMENTS
        {
            set { _code_comments = value; }
            get { return _code_comments; }
        }

        /// <summary>
        ///是否有效：0——使用（有效），1——禁用（失效） 
        /// </summary>
        [Property("IS_VALIDATE")]
        public int? IS_VALIDATE
        {
            set { _is_validate = value; }
            get { return _is_validate; }
        }

        /// <summary>
        /// 输入时间
        /// </summary>
        [Property("INPUT_TIME")]
        public DateTime? INPUT_TIME
        {
            set { _input_time = value; }
            get { return _input_time; }
        }
        /// <summary>
        /// 修改时间
        /// </summary>
        [Property("MODIFY_TIME")] 
        public DateTime? MODIFY_TIME
        {
            set { _modify_time = value; }
            get { return _modify_time; }
        }

        /// <summary>
        ///删除时间 
        /// </summary>
        [Property("DELETE_TIME")]
        public DateTime? DELETE_TIME
        {
            set { _delete_time = value; }
            get { return _delete_time; }
        }

        #endregion Model
        #region Static Methods

        protected override void OnDelete()
        {
            MKtCommCodelistHistory mHistory = new MKtCommCodelistHistory();
            Common.ModelCommon.CopyObjectValues(this, mHistory);
            mHistory.DELETE_TIME = DateTime.Now;
            mHistory.Create();
            base.OnDelete();
        }
        /// <summary>
        /// 创建一条记录
        /// </summary>
        /// <param name="commbase"></param>
        public static void Create(MKtCommCodeList codelist)
        {
            codelist.Create();
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="codelist"></param>
        public static void Update(MKtCommCodeList codelist)
        {
            codelist.Update();
        }
        /// <summary>
        /// 选择所有记录
        /// </summary>
        public static MKtCommCodeList[] SelectAll()
        {
            return (MKtCommCodeList[])FindAll(typeof(MKtCommCodeList));
        }

        /// <summary>
        /// 根据主键选择一条记录
        /// </summary>
        /// <param name="id"></param>
        public static MKtCommCodeList Select(int codeno)
        {
            return (MKtCommCodeList)FindByPrimaryKey(typeof(MKtCommCodeList), codeno);
        }
        /// <summary>
        /// 物理删除一条记录
        /// </summary>
        /// <param name="codelist"></param>
        /// <returns></returns>
        public static void Delete(MKtCommCodeList codelist)
        {
            codelist.Delete();
        }

        /// <summary>
        /// 逻辑删除一条记录
        /// </summary>
        /// <param name="codelist"></param>
        public static void DeletebyLogic(MKtCommCodeList codelist)
        {
        //    执行事务，持久化对象到数据库
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    codelist.Save();
                    tran.VoteCommit();
                }
                catch
                {
                    tran.VoteRollBack();
                }
            }
        }

        /// <summary>
        /// 通过typecode查找类型名称
        /// </summary>
        /// <param name="typecode"></param>
        /// <returns></returns>
        public static string GetTypeName(int typecode)
        {
            string hql = @"Select codetype.TYPE_NAME From MKtCommCodeType codetype Where codetype.TYPE_CODE=?";
            ScalarQuery query = new ScalarQuery(typeof(MKtCommCodeType), hql, typecode);
            return (string)ExecuteQuery(query);            
        }

        /// <summary>
        /// 通过类型名称查找符合该类型所有数据
        /// </summary>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static MKtCommCodeList[] GetCodeType(string typename)
        {
            string strhql = @"From MKtCommCodeList codelist where codelist.MKtCommCodeType.TYPE_NAME like '%'+?+'%'";
            SimpleQuery sq = new SimpleQuery(typeof(MKtCommCodeList), strhql, typename);
            return (MKtCommCodeList[])ExecuteQuery(sq);
        }

        /// <summary>
        /// 根据某Property列返回符合条件的记录条数
        /// </summary>
        /// <returns></returns>
        public static int SelectByProperty(string strPro, object Value)
        {
            MKtCommCodeList[] baseinfo;
            baseinfo = (MKtCommCodeList[])FindAllByProperty(typeof(MKtCommCodeList), strPro, Value);
            return baseinfo.Length;
        }
        /// <summary>
        /// 根据中文名称和类型编码返回该类型编码下该中文名称的个数
        /// </summary>
        /// <param name="cname"></param>
        /// <param name="typecode"></param>
        /// <returns></returns>
        public  static int CNameValidate(string cname,int typecode)
        {
            string strhql = @"select count(*) from MKtCommCodeList  codelist ";
            strhql += @"where codelist.CODE_CNAME= ? and codelist.MKtCommCodeType.TYPE_CODE=?";
            ScalarQuery query = new ScalarQuery(typeof(MKtCommCodeList), strhql, cname, typecode);
            return Convert.ToInt32(ExecuteQuery(query));
        }
        #endregion Static Methods

        /// <summary>
        /// 测试级联查询
        /// </summary>
        /// <returns></returns>

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static MKtCommCodeList[] List()
        {
            string strHql = @"Select codelist from  MKtCommCodeList codelist,MKtCommCodeType codetype";
            SimpleQuery query = new SimpleQuery(typeof(MKtCommCodeList), strHql);
            return (MKtCommCodeList[])ExecuteQuery(query);
        }

        /// <summary>
        /// 返回第start条记录后的max条记录
        /// </summary>
        /// <param name="start"></param>
        /// <param name="max"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static IList<MKtCommCodeList> FindALLSliced(int start, int max, string where)
        {
            MKtCommCodeList codelist = new MKtCommCodeList();
            string hql = @"from MKtCommCodeList codelist " + where;
            SimpleQuery query = new SimpleQuery(typeof(MKtCommCodeList), hql);
            query.SetQueryRange(start, max);
            return (IList<MKtCommCodeList>)ExecuteQuery(query);
        }
        /// <summary>
        /// 根据where条件返回符合条件记录数
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public static int GetCount(string where)
        {
            string hql = @"select count(*) from MKtCommCodeList codelist " + where;
            ScalarQuery query = new ScalarQuery(typeof(MKtCommCodeList), hql);
            return Convert.ToInt32(ExecuteQuery(query));
        }

        /// <summary>
        /// 根据where条件返回符合条件记录
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public static IList<MKtCommCodeList> SelectByWhere(string where)
        {
            string hql = @"from MKtCommCodeList codelist " + where;
            SimpleQuery query = new SimpleQuery(typeof(MKtCommCodeList), hql);
            return (IList<MKtCommCodeList>)ExecuteQuery(query);
        }

        ///// <summary>
        ///// Selects all.
        ///// </summary>
        ///// <returns></returns>
        //public static IList<MKtCommCodeList> SelectAll()
        //{
        //    return (IList<MKtCommCodeList>)FindAll(typeof(MKtCommCodeList));
        //}
        /// <summary>
        /// Selects all.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public static IList<MKtCommCodeList> SelectAll(params ICriterion[] criteria)
        {
            return (IList<MKtCommCodeList>)FindAll(typeof(MKtCommCodeList), criteria);
        }
        /// <summary>
        /// Selects all.
        /// </summary>
        /// <param name="orders">The orders.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public static IList<MKtCommCodeList> SelectAll(Order[] orders, params ICriterion[] criteria)
        {
            return (IList<MKtCommCodeList>)FindAll(typeof(MKtCommCodeList), orders, criteria);
        }
        /// <summary>
        /// Selects all.
        /// </summary>
        /// <param name="detachedCriteria">The detached criteria.</param>
        /// <param name="orders">The orders.</param>
        /// <returns></returns>
        public static IList<MKtCommCodeList> SelectAll(DetachedCriteria detachedCriteria, params Order[] orders)
        {
            return (IList<MKtCommCodeList>)FindAll(typeof(MKtCommCodeList), detachedCriteria, orders);
        }


        /// <summary>
        /// Selects the by primary key.
        /// </summary>
        /// <param name="ID">The ID.</param>
        /// <returns></returns>
        public static MKtCommCodeList SelectByPrimaryKey(object ID)
        {
            return (MKtCommCodeList)FindByPrimaryKey(typeof(MKtCommCodeList), ID, false);
        }


    }
}
